package com.ruoyi.business.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.FollowUpRecord;
import com.ruoyi.business.service.IFollowUpRecordService;
import com.ruoyi.business.vo.FollowUpRecordVO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.PotentialCustomer;
import com.ruoyi.business.service.IPotentialCustomerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 潜在客户Controller
 * 
 * @author zw
 * @date 2021-10-20
 */
@Controller
@RequestMapping("/business/customer")
public class PotentialCustomerController extends BaseController
{
    private String prefix = "business/customer";

    @Autowired
    private IPotentialCustomerService potentialCustomerService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IFollowUpRecordService followUpRecordService;

    @RequiresPermissions("business:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询潜在客户列表
     */
    @RequiresPermissions("business:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PotentialCustomer potentialCustomer)
    {
        startPage();

        List<PotentialCustomer> list = potentialCustomerService.selectPotentialCustomerList(potentialCustomer);
        return getDataTable(list);
    }

    /**
     * 导出潜在客户列表
     */
    @RequiresPermissions("business:customer:export")
    @Log(title = "潜在客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PotentialCustomer potentialCustomer)
    {
        List<PotentialCustomer> list = potentialCustomerService.selectPotentialCustomerList(potentialCustomer);
        ExcelUtil<PotentialCustomer> util = new ExcelUtil<PotentialCustomer>(PotentialCustomer.class);
        return util.exportExcel(list, "潜在客户数据");
    }

    /**
     * 新增潜在客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存潜在客户
     */
    @RequiresPermissions("business:customer:add")
    @Log(title = "潜在客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PotentialCustomer potentialCustomer)
    {
        PotentialCustomer customer = potentialCustomerService.selectUserByPhoneNumber(potentialCustomer.getPhone());
        if (customer != null){
            throw new BusinessException("用户已存在,不可创建");
        }
        potentialCustomer.setCreateTime(new Date());
        potentialCustomer.setLastFollowUpTime(new Date());
        potentialCustomer.setFollowUpUser(ShiroUtils.getSysUser().getUserId());
        return toAjax(potentialCustomerService.insertPotentialCustomer(potentialCustomer));
    }

    /**
 * 修改潜在客户
 */
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, ModelMap mmap)
{
    PotentialCustomer potentialCustomer = potentialCustomerService.selectPotentialCustomerById(id);
    SysUser sysUser = sysUserService.selectUserById(potentialCustomer.getFollowUpUser());
    mmap.put("followUpUserName",sysUser.getUserName());
    mmap.put("potentialCustomer", potentialCustomer);
    return prefix + "/edit";
}

    /**
     * 修改保存潜在客户
     */
    @RequiresPermissions("business:customer:edit")
    @Log(title = "潜在客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PotentialCustomer potentialCustomer)
    {
        potentialCustomer.setLastFollowUpTime(new Date());
        potentialCustomer.setFollowUpUser(ShiroUtils.getSysUser().getUserId());
        return toAjax(potentialCustomerService.updatePotentialCustomer(potentialCustomer));
    }

    /**
     * 删除潜在客户
     */
    @RequiresPermissions("business:customer:remove")
    @Log(title = "潜在客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(potentialCustomerService.deletePotentialCustomerByIds(ids));
    }
    /**
     * 跟进潜在客户
     */
    @GetMapping("/goToCustomer/{id}")
    public String goToCustomer(@PathVariable Long id, ModelMap map)
    {
        map.put("customerId",id);
        return prefix + "/addGotoCustomer";
    }

    /**
     * 新增保存跟进记录
     */
    @Log(title = "跟进记录", businessType = BusinessType.INSERT)
    @PostMapping("/addGotoCustomer")
    @ResponseBody
    public AjaxResult addSave(FollowUpRecord followUpRecord,Long customerId)
    {
        followUpRecord.setCustomerId(customerId);
        followUpRecord.setFollowUpTime(new Date());
        if (customerId != null){
            PotentialCustomer customer = potentialCustomerService.selectPotentialCustomerById(customerId);
            customer.setLastFollowUpTime(new Date());
            potentialCustomerService.updatePotentialCustomer(customer);
        }
        return toAjax(followUpRecordService.insertFollowUpRecord(followUpRecord));
    }

    @GetMapping("/show")
    public String carMaintenanceInfo(Long id, ModelMap map)
    {
        map.put("customerId", id);
        return prefix + "/showGotoCustomerHistory";
    }
    /**
     * 跟进潜在客户历史
     */
    @PostMapping("/showGotoCustomerHistory")
    @ResponseBody
    public TableDataInfo showGoToCustomerHistory( Long customerId)
    {
        startPage();
        List<FollowUpRecordVO> followUpRecordList = followUpRecordService.selectFollowUpRecordByCustomerId(customerId);
        return getDataTable(followUpRecordList);
    }


    /**
     * 删除跟进记录
     */
    @Log(title = "删除跟进记录", businessType = BusinessType.DELETE)
    @PostMapping( "/removeFlag")
    @ResponseBody
    public AjaxResult removeFlag(String ids)
    {
        return toAjax(followUpRecordService.deleteFollowUpRecordByIds(ids));
    }
}
